<template>
  <div>
    <!-- 一条文章单元格 -->
    <van-cell>
      <!-- 标题区域的插糟 -->
      <template #title>
        <div class="title-box">
          <span>{{ artItem.title }}</span>
          <!-- 单图 -->
          <img
            v-if="artItem.cover.type === 1"
            class="thumb"
            v-lazy="artItem.cover.images[0]"
            alt=""
          />
        </div>
        <!-- 多图 -->
        <div class="thumb-box" v-if="artItem.cover.type > 1">
          <img
            v-for="(item, index) in artItem.cover.images"
            :key="index"
            v-lazy="item"
            alt=""
            class="thumb"
          />
        </div>
      </template>
      <!-- label区域 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{ artItem.aut_name }}</span>
            <span>{{ artItem.comm_count }}评论</span>
            <span>{{ relativeTime(artItem.pubdate) }}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" @click.stop="show = true" v-if="isShow"></van-icon>
        </div>
      </template>
    </van-cell>
    <van-action-sheet
      v-model="show"
      :actions="actions"
      @select="onSelect"
      @cancel="onCancel"
      @click-overlay="onOverlay"
      get-container="body"
      :cancel-text="cancelText"
      :close-on-click-overlay="overlay"
    />
  </div>
</template>

<script>
import { relativeTime } from '@/utils/relativeTime'
import { firstActions, secondActions } from '@/api/report'
import { articleDislikes, articleReports } from '@/api/index'
import { Notify } from 'vant'

export default {
  name: 'ArticleItemVue',
  props: {
    artItem: Object, // 文章单个对象
    isShow: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      show: false,
      cancelText: '取消', // 面板按钮
      actions: firstActions, // 面板选项
      overlay: true // 点击遮罩层是否关闭面板状态
    }
  },
  methods: {
    async onSelect (action) {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      if (action.name === '反馈垃圾内容') {
        this.overlay = false
        this.cancelText = '返回'
        this.actions = secondActions
        return
      } else if (action.name === '不感兴趣') {
        // 不感兴趣的文章
        try {
          const res = await articleDislikes(this.artItem.art_id)
          if (res.data.message === 'OK') {
            this.$emit('delItem', res.data.data.target)
            Notify({ type: 'success', message: '提交成功' })
          }
        } catch (err) {}
      } else {
        // 举报文章
        const res = await articleReports({
          target: this.artItem.art_id,
          type: action.name
        })
        if (res.data.message === 'OK') {
          this.$emit('delItem', res.data.data.target)
          Notify({ type: 'success', message: '提交成功' })
        }
      }
      this.show = false
    },
    // 点击取消和返回按钮判断
    onCancel () {
      if (this.cancelText === '返回') {
        this.cancelText = '取消'
        this.actions = firstActions
        return
      }
      this.show = false
    },
    // 点击遮罩层事件
    onOverlay () {
      if (this.cancelText === '返回') {
        this.cancelText = '取消'
        this.actions = firstActions
      }
      this.overlay = true
    },
    relativeTime
  }
}
</script>

<style lang="less" scoped>
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  span {
    margin: 0 3px;
    &:first-child {
      margin-left: 0;
    }
  }
}

// 图片的样式，矩形黄金比例： 0.618
.thumb {
  widows: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}
// 三图，图片容器
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
